<?php
/**
 * @file bvlgaricontroller.php
 * @author LinLin
 * @date 2016年3月21日 下午12:04:12
 * @brief 
 */
namespace haibao\cms\business\control\pc\channel\bvlgari;

class BvlgariController extends \haibao\cms\business\control\pc\channel\ChannelController{

    public function __construct($updateTime = null,$controlId,$isPublish){
        parent::__construct($updateTime,$controlId,$isPublish);
    }

    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $articleInfo =  array();

        if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'calendar'){
            $parameters = $auspiciousDay = array();
            $mainDay = $zhudaImgUrlOne = $zhudaImgUrlTwo = $starNameOne = $starNameTwo = $mainXingQi = $yearDate = '';
            if($controlData['auto']){
                foreach($controlData['auto'] as $data){
                    $parameterValue = json_decode($data['ParameterValue']);
                    array_push($parameters,$parameterValue);
                }
            }
            
            $nowMonth = date('m',time());//当前月份
            $nowYear = date('Y',time());//当前年份
            $arr = $this->getCurrentMonthDayArr();//当月日期数组
            if($parameters){
                foreach($parameters as $key=>$val){
                    if($val->month == $nowMonth && $val->year == $nowYear){
                        $auspiciousDay = explode(',',str_replace("，",",",$val->auspiciousDay));//当月吉日
                        $mainDay = $val->mainDay;
                        $zhudaImgUrlOne = $val->imgUrlOne;
                        $zhudaImgUrlTwo = $val->imgUrlTwo;
                        $starNameOne = $val->starNameOne;
                        $starNameTwo = $val->starNameTwo;
                        $yearDate = $val->year;
                    }
                }
                if(!empty($auspiciousDay)){
                    $auspiciousDay = array_filter($auspiciousDay);
                    if(in_array($mainDay,$auspiciousDay)){
                        foreach($auspiciousDay as $kk=>$vv){
                            if($auspiciousDay[$kk] == $mainDay){
                                unset($auspiciousDay[$kk]);
                            }
                        }
                    }
                    $arr = $this->getMergeArr($arr,$auspiciousDay); //取合并数组（带有吉日的数组）
                }
            }
            $dayNum = count($arr);
            $rows = ceil($dayNum/7);
            $newArr = array();
            $n = 0;
            for($m=0;$m<$rows;$m++){
                $newArr[] = array_slice($arr, $n,7);
                $n = $n+7;
            }
            
            $engMonth = array(
                '1'=>'Jan',
                '2'=>'Feb',
                '3'=>'Mar',
                '4'=>'Apr',
                '5'=>'May',
                '6'=>'Jun',
                '7'=>'Jul',
                '8'=>'Aug',
                '9'=>'Sep',
                '10'=>'Oct',
                '11'=>'Nov',
                '12'=>'Dec'
            );
            
            $nowDay = date('d',time()); //当天日期
            $nowYearMonth = date('Y-m',time());//当前月份
            if($mainDay){
                $mainXingQi = date("w",strtotime($nowYearMonth.'-'.$mainDay));
            }
            $this->assign('newArr', $newArr);
            $this->assign('mainDay', $mainDay);
            $this->assign('currentMonth', (int)$nowMonth);
            $this->assign('zhudaImgUrlOne', $zhudaImgUrlOne);
            $this->assign('zhudaImgUrlTwo', $zhudaImgUrlTwo);
            $this->assign('starNameOne', $starNameOne);
            $this->assign('starNameTwo', $starNameTwo);
            $this->assign('mainXingQi', $mainXingQi);
            $this->assign('nowYear', $nowYear);
            $this->assign('engMonth', json_encode($engMonth));
            $this->assign('engMonthObj', $engMonth);
            $this->assign('parameters', json_encode($parameters));
        }
        
        if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'articleLists'){
            $bvlgariData = new \haibao\cms\data\control\bvlgari\BvlgariController();
            $articleInfo = $bvlgariData->getArticlesByTagId(211);
            $this->assign('articleInfo', $articleInfo);
        }
        $isNewHtml = $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    //当月日期数组
    private function getCurrentMonthDayArr(){
        $days = date("t");//本月天数
        $arr=array();
        for($i=1;$i<=$days;$i++){
            $arr[]=$i;   //本月日期数组
        }
        $xingqi = Date("w", strtotime(Date("Y-n-1")));//当月第一天是星期几
        if($xingqi < 7){
            for($i=0;$i<$xingqi;$i++){
                array_unshift($arr, '');//本月日期数组(加空值)
            }
        }
        return $arr;
    }
    
    //取合并数组（带有吉日的数组）
    private function getMergeArr($arr,$auspiciousDay){
        $temp = array();
        foreach ($arr as $key=>$val){
            foreach($auspiciousDay as $k=>$v){
                if($val == $v){
                    $temp[0] = $val;
                    $arr[$key] = $temp;
                }
            }
        }
        return $arr;
    }

} 